package  code.ws_common_scene.texturedata
{
	import code.ws_common_ems.ModeActionEms;
	import code.ws_common_utils.SysUtils;
	import code.ws_common_utils.UrlUtils;
	
	import flash.utils.Dictionary;

	/**
	 * 模型动作数据池 
	 * @author qiangchen
	 * 
	 */	
	public class ModeTextureDataPool
	{
		/** 数据池 */
		private static var _dataDic:Dictionary = new Dictionary();
		/** 方向 */
		private static const analyzeDirNum:int = 8;
		/** 受伤动作取死亡动作的前两帧 */
		private static const sliceLen:int = 2;
		
		/**
		 * 获取播放数据 
		 * @param render
		 * @param style
		 * @param action
		 * @param dir
		 * @return 
		 * 
		 */		
		public static function getPlayData( render:String, style:String, action:String, dir:int ):TexturePlayData
		{
			if( !_dataDic[render] ) return null;
			
			if( !_dataDic[render][ style ] ) return null;
			
			if( !_dataDic[render][style][action] ) return null;
			
			return _dataDic[render][style][action][dir] as TexturePlayData;
		}
		
		
		/**
		 * 解析动作数据 
		 * @param render
		 * @param xml
		 * 
		 */		
		public static function iniAnalyze( render:String, xml:XML ):void
		{
			if( render == null || render == "" )
			{
				throw new Error("ModeTextureDataPool --->(error): render value is null or \"\".");
				return;
			}
			
			// 创建不同的render组
			if( !_dataDic[render] )
			{
				_dataDic[render] = new Dictionary();
			}
			
			var _xml:XML = xml;
			var lens:int = _xml.info.length();
			var _$xml:XML = null;
			var _$xml2:XML = null;
			var actionNum:int;
			var _$xml3:XML = null;
			for( var i:int=0; i<lens; i++ )
			{
				_$xml = XML( _xml.info[i] );
				var style:String = _$xml.nameid;
				
				if( _dataDic[render][ style ] )
				{
					continue;
				}
				
				_dataDic[render][style] = new Dictionary();
				_$xml2 = XML(_$xml.actionDatas);
				actionNum = _$xml2.elements().length();
				if( actionNum == 0 )
				{
					continue;
				}
				
				for( var j:int=0; j<actionNum; j++ )
				{
					_$xml3 = XML( _$xml2.child( j ) );
//					trace( _$xml3 );
					var actions:String = _$xml3.localName();
					if( _dataDic[render][style][actions] )
					{
						continue;
					}
					_dataDic[render][style][actions] = new Dictionary();
					
					// 8 direction
					iniDirData( _$xml3, render, style, actions );
				}
			}
			
			// dispose
			_xml = null;
			lens = -1;
			_$xml = null;
			_$xml2 = null;
			actionNum = -1;
			_$xml3 = null;
		}
		
		
		private static function iniDirData( xml:XML, render:String, style:String, actions:String ):void
		{
			for( var m:int=0; m<analyzeDirNum; m++ )
			{
				var playData:TexturePlayData = new TexturePlayData();
				playData.frameCount = xml.@frameCount;
				playData.frameRate = xml.@frameRate;
				playData.keyFrame = xml.@keyFrame;
				playData.style = style;
				playData.action = actions;
				playData.direction = m;
				playData.isLoop = ( actions == ModeActionEms.ATTACK || actions == ModeActionEms.DEAD ) ? false : true;
				
				var startIndex:int = getRealDir(playData.direction) * playData.frameCount;
				for( var n:int=0; n<playData.frameCount; n++ )
				{
					var _xml:XML = XML( xml.child( "f" + (startIndex + n) ) );
					if(  _xml == null ) continue;
					
					var texture:Textures = new Textures();
					texture.w = _xml.@width;
					texture.h = _xml.@height;
					texture.scal = getRealScale( m );
					texture.offx = ( texture.scal == 1 ) ? _xml.@x : -_xml.@x ;
					texture.offy = _xml.@y;
					texture.key = playData.style + "_" + playData.action;
					texture.path = UrlUtils.getHerolUrl( texture.key );
					texture.key +=  ("_" + (startIndex + n + 1));
					playData.framesTimeLine.push( playData.frameRate * 1000 / SysUtils.stage.frameRate );
					playData.textures.push( texture );
				}
				
				playData.ini();
				_dataDic[render][style][actions][playData.direction] = playData;
			}
			
			/* 添加特殊的动作 */
			if( getPlayData( render, style, ModeActionEms.DEAD, 0 ) && !getPlayData(render, style, ModeActionEms.HURT, 0) )
			{
				cHurtActions(render, style);
			}
		}
		
		/**
		 * 创建受伤动作播放数据 
		 * @param render
		 * @param style
		 * 
		 */		
		private static function cHurtActions( render:String, style:String ):void
		{
			for( var i:int=0; i<analyzeDirNum; i++ )
			{
				var playData:TexturePlayData = getPlayData( render, style, ModeActionEms.DEAD, i );
				var hurtPlaydata:TexturePlayData = new TexturePlayData();
				hurtPlaydata.action = ModeActionEms.HURT;
				hurtPlaydata.style = style;
				hurtPlaydata.direction = i;
				hurtPlaydata.frameCount = sliceLen;
				hurtPlaydata.frameRate = playData.frameRate * sliceLen;
				hurtPlaydata.keyFrame = 0;
				hurtPlaydata.isLoop = false;
				for( var j:int=0; j<sliceLen; j++ )
				{
					if( j > playData.textures.length -1 ) continue;
					hurtPlaydata.textures.push( playData.textures[j] );
					hurtPlaydata.framesTimeLine.push( playData.framesTimeLine[j] );
				}
				hurtPlaydata.ini();
				
				if( !_dataDic[render][style][ModeActionEms.HURT] )
				{
					_dataDic[render][style][ModeActionEms.HURT] = new Dictionary();
				}
				_dataDic[render][style][ModeActionEms.HURT][playData.direction] = hurtPlaydata;
				
				// 释放引用
				hurtPlaydata = null;
				playData = null;
			}
		}
		
		
		/**
		 * 分析方向 
		 * @param dir
		 * @return 
		 * 
		 */		
		private static function getRealDir( dir:int ):int
		{
			if( dir <= 4 )
			{
				return dir;
			}
			
			if( dir == 5 )
			{
				return 3;
			}
			else if( dir == 6 )
			{
				return 2;
			}
			else if( dir == 7 )
			{
				return 1;
			}
			
			return 4;
		}
		
		/**
		 * 分析scale值 
		 * @param dir
		 * @return 
		 * 
		 */		
		private static function getRealScale( dir:int ):int
		{
			if( dir <= 4 )
			{
				return 1;
			}
			
			if( dir == 5 || dir == 6 || dir == 7 )
			{
				return -1;
			}
			
			return 1;
		}
		
		/**
		 * 获取数据池 
		 * @return 
		 * 
		 */		
		public static function get dataDic():Dictionary
		{
			return _dataDic;
		}
	}
}